package com.shengde.jinwawaforleader.model.homePage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProInfoDetail;
import com.shengde.jinwawaforleader.utils.UmengShareUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 公告详情
 * Created by Administrator on 2016/9/7 0007.
 */
public class SchoolNewsInformationItemActivity extends BaseActivity {
	private static final String TAG = "InformationItemActivity";
	private static final int FINSH = 0;
	private TextView tv_back; //
	private TextView tv_title;
	public WebView show_wv;
	private TextView tv_share; //分享
	private String noticeid;
	private String imgpath;
	private LinearLayout loading_main;  //加载中..
	private Long time;
	private Long lastTime;
	private String title;

	private String targeturl;
	private String setting; ////这个字符串为了让webview图片适应屏幕
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					loading_main.setVisibility(View.GONE);
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.informationg_activity);

	}

	@Override
	public void initView() {
		loading_main = (LinearLayout) findViewById(R.id.loading_main);

		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_share = (TextView) findViewById(R.id.fill_add);
		show_wv = (WebView) findViewById(R.id.show_wv);


	}


	@Override
	public void initData() {
		tv_share.setVisibility(View.VISIBLE);
		tv_share.setText(getString(R.string.share));
		title = getIntent().getStringExtra("noticeTitle");
		noticeid = getIntent().getStringExtra("noticeId");
		imgpath = getIntent().getStringExtra("imgpath");
		Log.i(TAG, "initData: lasttime" + getIntent().getLongExtra("lastTime", 123));
		time = getIntent().getLongExtra("lastTime", 0);
		lastTime = changeTime();
		if (title.length() > 8) {
			tv_title.setText(title);
			tv_title.setSingleLine();
			tv_title.setEms(8);
		} else {
			tv_title.setText(title);
		}

		Log.i(TAG, "initData: noticeid" + noticeid);
		targeturl = "http://www.kingvar.com/soldiery/news.htm?infoid=" + noticeid;
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		int width = metric.widthPixels; // 屏幕宽度（像素）
		float density = metric.density;

		setting = "<head><style>img{width:" + width / density + " !important;}</style></head>";

		WebSettings settings = show_wv.getSettings();
		settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		settings.setBuiltInZoomControls(true);//在可缩放页面，支持放大缩小功能
		settings.setSupportZoom(true);
		settings.setDisplayZoomControls(false);
		settings.setDomStorageEnabled(true);
		showHotNewsDetail(noticeid);

	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		tv_share.setOnClickListener(this);

	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				String url =  BaseProtocol.IMG_BASE + imgpath;
				UmengShareUtil.shareUrl(SchoolNewsInformationItemActivity.this,title,title,url,targeturl);
				break;
		}

	}
	/**
	 * 时间转换为时间戳
	 *
	 * @return
	 */
	private static Long changeTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = "2016-09-27 10:00:00";
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date.getTime();
	}
	/**
	 * 获取热门咨询详情
	 *
	 * @param noticeid
	 */
	private void showHotNewsDetail(String noticeid) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/teacher/NoticeDetail";
		} else {
			url = BaseProtocol.HOST_BASE + "/teacher/NoticeDetail";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("noticeid", noticeid);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProInfoDetail pro_info_detail = new Gson().fromJson(result, ProInfoDetail.class);
				if (pro_info_detail != null && pro_info_detail.getCode() == 0) {

					//解码 表情内容
					String emocontent = "";
					try {
						emocontent = URLDecoder.decode(pro_info_detail.getDetail(), "UTF-8");
					} catch (Exception e) {
						// TODO: handle exception
					}

					Log.i(TAG, "onEndWhileMainThread: emocontent" + emocontent);
					Log.i(TAG, "onEndWhileMainThread: detail" + pro_info_detail.getDetail());
					//为了判断是不是苹果手机发的
					int isIos = emocontent.indexOf("background-color");
					Log.i(TAG, "onSuccess: time" + time);
					Log.i(TAG, "onSuccess: lasttime" + lastTime);
					Log.i(TAG, "onSuccess: isIos" + isIos);
					if (time < lastTime && isIos == -1) {
						show_wv.loadDataWithBaseURL(null, setting + emocontent,
								"text/html", "utf-8", null);

					} else {
						if (emocontent == null || "".equals(emocontent)) {
							show_wv.loadDataWithBaseURL(null, setting + pro_info_detail.getDetail(),
									"text/html", "utf-8", null);
						} else {
							show_wv.loadDataWithBaseURL(null, setting + emocontent,
									"text/html", "utf-8", null);
						}

					}

					Log.i(TAG, "onSuccess:内容 " + pro_info_detail.getDetail());
					mHandler.sendEmptyMessage(FINSH);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
